package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ElderlyDTO;
import com.api.shopapi.service.ActivityPeopleService;
import com.api.shopapi.service.ArticleService;
import com.api.shopapi.service.CardStatusService;
import com.api.shopapi.service.ElderlyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;


/**
 * 活动参与的人
 * */

@RestController
@RequestMapping("ActivityPeople")
public class ActivityPeopleController extends BaseController{

    @Autowired
    @Qualifier("ActivityPeopleServiceImplOne")
    private ActivityPeopleService activityPeopleService;

    @Autowired
    @Qualifier("ArticleServiceImplOne")
    private ArticleService articleService;

    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;

    @Autowired
    @Qualifier("CardStatusServiceImplOne")
    private CardStatusService cardStatusService;

    @RequestMapping(value = "Add", method = RequestMethod.POST)
    @Transactional
    public MyResult Add(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String idCard = jsonObject.getString("idCard");
            String name = jsonObject.getString("name");
            String phone = jsonObject.getString("phone");
            String articleCode = jsonObject.getString("articleCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityPeople/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ElderlyDTO elderlyDTO = elderlyService.getByPhone(phone);
            ElderlyDTO elderlyDTO1 = elderlyService.getByIdCard(idCard);
            if (elderlyDTO==null&&elderlyDTO1==null){
                return failed("未查询到老人信息,请先进行注册");
            }

            if (activityPeopleService.getByPhone(phone,articleCode)!=null&&!phone.equals("")){
                return failed("电话号码重复");
            }

            if (activityPeopleService.getByIdCard(idCard,articleCode)!=null&&!idCard.equals("")){
                return failed("身份证号码重复");
            }

            ActivityPeople activityPeople=new ActivityPeople();
            activityPeople.setActivityPeopleCode(UUID.randomUUID().toString());
            if (elderlyDTO!=null){
                activityPeople.setElderlyCode(elderlyDTO.getElderlyCode());
            }

            if (elderlyDTO1!=null){
                activityPeople.setElderlyCode(elderlyDTO1.getElderlyCode());
            }

            activityPeople.setArticleCode(articleCode);
            activityPeople.setName(name);
            if (!idCard.equals("")){
                activityPeople.setIdCard(idCard);
            }
            if (!phone.equals("")){
                activityPeople.setPhone(phone);
            }
            activityPeople.setCreateTime(new Date());
            activityPeopleService.insert(activityPeople);

            Article article = articleService.getByArticleCode(articleCode);
            article.setJoinPeople(article.getJoinPeople()+1);
            articleService.update(article);

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertErrorForAffair("/ActivityPeople/Add!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "AddByIcCard", method = RequestMethod.POST)
    @Transactional
    public MyResult AddByIcCard(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String icCard = jsonObject.getString("icCard");
            String articleCode = jsonObject.getString("articleCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityPeople/AddByIcCard");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Elderly elderly = elderlyService.getByIcCard(icCard);

            CardStatus cardStatus = cardStatusService.getByCardNumber(icCard);
            if (cardStatus.getStatus()==2){
                return failed("改卡已被冻结");
            }

            ActivityPeople activityPeople1 = activityPeopleService.getByElderlyCodeAndArticleCode(elderly.getElderlyCode(), articleCode);
            if (activityPeople1!=null){
                return failed("该老人已经签到过此活动");
            }




            ActivityPeople activityPeople=new ActivityPeople();
            activityPeople.setActivityPeopleCode(UUID.randomUUID().toString());
            activityPeople.setElderlyCode(elderly.getElderlyCode());
            activityPeople.setCreateTime(new Date());
            activityPeople.setIdCard(elderly.getIdCard());
            activityPeople.setPhone(elderly.getPhone());
            activityPeople.setArticleCode(articleCode);
            activityPeople.setName(elderly.getName());

            activityPeopleService.insert(activityPeople);


            Article article = articleService.getByArticleCode(articleCode);
            article.setJoinPeople(article.getJoinPeople()+1);
            articleService.update(article);

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertErrorForAffair("/ActivityPeople/AddByIcCard!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String idCard = jsonObject.getString("idCard");
            String name = jsonObject.getString("name");
            String phone = jsonObject.getString("phone");
            String activityPeopleCode = jsonObject.getString("activityPeopleCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityPeople/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ActivityPeople activityPeople = activityPeopleService.getByActivityPeopleCode(activityPeopleCode);

            ActivityPeople activityPeople1 = activityPeopleService.getByPhone(phone, activityPeople.getArticleCode());
            if (activityPeople1!=null&&!phone.equals(activityPeople.getPhone())&&!phone.equals("")){
                return failed("电话号码重复");
            }
            ActivityPeople activityPeople2 = activityPeopleService.getByIdCard(idCard, activityPeople.getArticleCode());
            if (activityPeople2!=null&&!idCard.equals(activityPeople.getIdCard())){
                return failed("身份证号码重复");
            }

            activityPeople.setIdCard(idCard);
            activityPeople.setName(name);
            activityPeople.setPhone(phone);
            activityPeopleService.update(activityPeople);

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
//            e.printStackTrace();
            insertError("/ActivityPeople/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "Delete", method = RequestMethod.POST)
    @Transactional
    public MyResult Delete(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String activityPeopleCode = jsonObject.getString("activityPeopleCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityPeople/Delete");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ActivityPeople activityPeople = activityPeopleService.getByActivityPeopleCode(activityPeopleCode);
            activityPeopleService.delete(activityPeopleCode);
            Article article = articleService.getByArticleCode(activityPeople.getArticleCode());
            article.setJoinPeople(article.getJoinPeople()-1);
            articleService.update(article);

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertErrorForAffair("/ActivityPeople/Delete!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "DeleteAll", method = RequestMethod.POST)
    @Transactional
    public MyResult DeleteAll(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            JSONArray activityPeopleCodeList = jsonObject.getJSONArray("activityPeopleCodeList");
            String articleCode = jsonObject.getString("articleCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityPeople/DeleteAll");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            for (int i=0;i<activityPeopleCodeList.size();i++){
                activityPeopleService.delete(activityPeopleCodeList.get(i).toString());
            }
            Article article = articleService.getByArticleCode(articleCode);
            article.setJoinPeople(article.getJoinPeople()-activityPeopleCodeList.size());
            articleService.update(article);

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertErrorForAffair("/ActivityPeople/DeleteAll!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getIntValue("pageIndex");
            int pageSize = jsonObject.getIntValue("pageSize");
            String articleCode = jsonObject.getString("articleCode");
            String name = jsonObject.getString("name");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityPeople/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PageInfo<ActivityPeople> activityPeoplePageInfo = activityPeopleService.getPage(pageIndex, pageSize, articleCode,name);

            return success(activityPeoplePageInfo);
        } catch (Exception e) {
            insertError("/ActivityPeople/Page",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "PageToGovPlatform", method = RequestMethod.POST)
    public MyResult PageToGovPlatform(@RequestBody JSONObject jsonObject) {

        try {

            int pageIndex = jsonObject.getIntValue("pageIndex");
            int pageSize = jsonObject.getIntValue("pageSize");
            String articleCode = jsonObject.getString("articleCode");
            String name = jsonObject.getString("name");

            PageInfo<ActivityPeople> activityPeoplePageInfo = activityPeopleService.getPage(pageIndex, pageSize, articleCode,name);

            return success(activityPeoplePageInfo);
        } catch (Exception e) {
            insertError("/ActivityPeople/PageToGovPlatform",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }



}
